package com.fhj.movie.customespinner;


import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.fhj.movie.entity.JyjCircleListEntity;
import com.fhj.movie.entity.JyjCityAllInfoEntity;
import com.fhj.movie.entity.JyjStationListEntity;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.ListOrder;


public class CustomSpinner extends Spinner implements android.widget.AdapterView.OnItemClickListener, OnClickListener {

	public interface MySpinnerCallback{
		public void returndata(String name,long id);
		
	}
	
	MySpinnerCallback callback =null;
	
	public void setonClick(MySpinnerCallback callback){
		this.callback = callback;
	}
	
	
	public static  CustomDialog dialog=null;
	public int districtId;
	private ListView listView1;
	private ListView listView2;
	private TextView shangquan;
	private TextView ditie;
	private Context context;
	
	private JyjCityAllInfoEntity  allInfoEntity;//所有城市信息
	private List<JyjCircleListEntity> filtering;//排序后的集合
	private MovieRightAdapter rAdapter;
	private MovieLeftAdapter ladapter;
	private MovieSubwayLeftAdapter subwayLeftAdapter;
	private MovieSubwayRightAdapter subwayRightAdapter;
	private  int type = 0;
	
	

	//传入所有城市信息
	public void setAllInfoEntity(JyjCityAllInfoEntity allInfoEntity) {
		this.allInfoEntity = allInfoEntity;
	}
	
	public CustomSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	public boolean performClick() {
		
		
		Context context=getContext();
		final LayoutInflater inflater=LayoutInflater.from(context);
		final View  view =inflater.inflate(R.layout.movie_customspinner_listview, null);
	
		shangquan = (TextView)view.findViewById(R.id.movie_spinner_shangquan);
		shangquan.setTextColor(Color.rgb(255, 153, 0));//设置为默认的显示
		shangquan.setBackgroundResource(R.drawable.bg_btn_filter_type);
		
		ditie = (TextView)view.findViewById(R.id.movie_spinner_ditie);
		shangquan.setOnClickListener(this);
		ditie.setOnClickListener(this);
		
		listView1 = (ListView)view.findViewById(R.id.movie_customspinner_listview1);
		listView2 = (ListView)view.findViewById(R.id.movie_customspinner_listview2);
		
		if (listView1.getCount()>0) {
			listView1.removeAllViews();
		}
		if (listView2.getCount()>0) {
			listView2.removeAllViews();
		}
		
		if (allInfoEntity==null) {
			return false;
		}
		ladapter = new MovieLeftAdapter();
		rAdapter = new MovieRightAdapter();
		
		subwayLeftAdapter = new MovieSubwayLeftAdapter();
		subwayRightAdapter = new MovieSubwayRightAdapter();
		
		showShangquan(context);//默认展示商圈的内容
		
		dialog=new CustomDialog(context);
		LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		dialog.addContentView(view, params);
		 
		return  true;//为真，被分配OnClickListener，则返回，否则为false。
	}

	private List<JyjStationListEntity> entity;
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		if (parent==listView1&&type==0) {
			
			filtering = ListOrder.filtering(allInfoEntity.getJyjDistrictEntities().get(position).getId(), allInfoEntity.getCircleListEntities());
			rAdapter.init(getContext(), filtering);
			rAdapter.notifyDataSetChanged();
		}
		
		
		if (parent==listView1&&type==1) {
			entity =  allInfoEntity.getSubwayListEntities().get(position).getStationListEntities();
			subwayRightAdapter.init(context,entity);
			subwayRightAdapter.notifyDataSetChanged();
		}
		
		if (dialog!=null&&parent==listView2&&type==0) {
			
			setDistrictId(filtering.get(position).getDistrctId());
			
			callback.returndata(filtering.get(position).getName(), filtering.get(position).getDistrctId());
			
			dialog.dismiss();
			dialog=null;
		}
		if (dialog!=null&&parent==listView2&&type==1) {
			callback.returndata(entity.get(position).getName(),(long)entity.get(position).getId());
			
			dialog.dismiss();
			dialog=null;
		}
	}
	
	public int  getDistrictId() {
		return districtId;
	}

	public void setDistrictId(int  districtId) {
		this.districtId = districtId;
	}
	
	@Override
	public void onClick(View v) {
		if (v.getId() == shangquan.getId()) {
			shangquan.setTextColor(Color.rgb(255, 153, 0));
			shangquan.setBackgroundResource(R.drawable.bg_btn_filter_type);
			ditie.setTextColor(Color.BLACK);
			ditie.setBackgroundColor(Color.TRANSPARENT);
			type=0;
			showShangquan(context);
		}else {
			shangquan.setTextColor(Color.BLACK);
			shangquan.setBackgroundColor(Color.TRANSPARENT);
			ditie.setTextColor(Color.rgb(255, 153, 0));
			ditie.setBackgroundResource(R.drawable.bg_btn_filter_type);
			type=1;
			showSubway(context);
		}
	}
	
	/**
	 * 展示商圈的信息
	 * @param context
	 */
	private void showShangquan(Context context) {
		if (allInfoEntity.getJyjDistrictEntities()!=null) {
			ladapter.init(context, allInfoEntity.getJyjDistrictEntities());
			listView1.setAdapter(ladapter);
			listView1.setOnItemClickListener(this);
		}
		
		if (allInfoEntity.getCircleListEntities()!=null) {
			filtering = ListOrder.filtering(allInfoEntity.getJyjDistrictEntities().get(0).getId(), allInfoEntity.getCircleListEntities());
			rAdapter.init(context, filtering);
			listView2.setAdapter(rAdapter);
			listView2.setOnItemClickListener(this);
		}
	}
	
	private  void showSubway(Context  context){
		
		List<JyjStationListEntity> stationListEntities = allInfoEntity.getSubwayListEntities().get(0).getStationListEntities();
		
		if (allInfoEntity.getSubwayListEntities()!=null) {
			subwayLeftAdapter.init(context, allInfoEntity.getSubwayListEntities());
			listView1.setAdapter(subwayLeftAdapter);
			listView1.setOnItemClickListener(this);
		}
		
		//在第二列展示第一列的第一行的数据
		if (stationListEntities!=null) {
			entity = allInfoEntity.getSubwayListEntities().get(0).getStationListEntities() ;
			subwayRightAdapter.init(context, stationListEntities);
			listView2.setAdapter(subwayRightAdapter);
			listView2.setOnItemClickListener(this);
		}
	}
	
}
